/*
 * table_CommandList.c
 *
 *  Created on: 2012/06/29
 *      Author: guanxiaoying
 */
#include "../../parser/coredef.h"
#include "../../parser/cg/cg.h"

typedef struct _COMMANDLIST
{
	U2 kind;
	BOOL bInLoop;
	AST astCodeScp;
	AST astType;
	AST astFunc;
	AST astLeft;
	AST astRight;
	AST astNext;
	AST astPrev;
	/*debug,12/10/10,S*/
	BOOL flgChg;
	/*debug,12/10/10,E*/
#ifdef RECORD_CMDLIST_LABEL
	CHAR name[_BUFFER_SIZE_+4];
#endif
} COMMANDLIST, *PCOMMANDLIST;

#define TableSize  (sizeof(COMMANDLIST ))
#define TableType  PCOMMANDLIST
#define stTable stCommandListManager.memoryBlocks
#define maxTable stCommandListManager.maxRecordNum
#define  maxNumPerBlk  (stCommandListManager.astMaxNum_Block)

static TABLEMANAGER stCommandListManager = {
//		U4                  usIdx;
		0,
//		PSZ				    con_pszName;    constant
		__FILE__,
//		U2					con_usSize;		constant
		TableSize,
//		U2					block_num;	　　　　　　　Reset clear  際　要
		0,
//		S4					astMaxNum_Block;     １回設定のみ
		0,
//		S4					astTabNum_LastPage; 　　　　Reset clear  際　要
		0,
//		S4					astMaxNum_LastPage;     Reset clear  際　要
		0,
//		U4					ulMaxByte;	　　　　　　　　　　クリアしない
		0,
//		U4					con_minPageSize;　　　　　　　　　　constant
		0
};
static VOID resetListener( U4  procid, U2 index )
{
	if( procid == stCommandListManager.u4Idx )
	{
		if( index > 0 && index < MAX_VOLUMN_OF_TABLE )
		{
			stTable[ index-1] = NULL;
		}
	}
}
static TableType core_getTable( AST index )
{
	TableType temp = (TableType)Check_Com( &stCommandListManager, resetListener,  index );
	if( temp != NULL ){ return &temp[index%maxNumPerBlk]; } return NULL;
}
static BOOL ClearTable( VOID )
{
	return Clear_Com( &stCommandListManager );
}
static BOOL ResetTable( VOID )
{
	return Reset_Com( &stCommandListManager );
}
static BOOL AllocTable( AST astReqArg )
{
	return AllocTable_Com( &stCommandListManager, astReqArg, resetListener );
}

static AST getMaxLineNum( VOID ){
	return maxTable;
}
/* give up the way that defining get/set function pointer in the struct,
 * because of memory volume.
 */

static U4  getOpKind( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->kind;
}
/* set method */
static U4  setOpKind( AST index, U4 value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, value );
		return 0;
	}
	return pTbl->kind = value;
}
//	IDX_TYPEDEFINFO
static AST  getTypeInfo( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->astType;
}
/* set method */
static AST  setTypeInfo( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, value );
		return 0;
	}
	return pTbl->astType = value;
}
#ifdef RECORD_CMDLIST_LABEL
//	IDX_SYMNAME	STR_SYMNAME
static CHAR*  getLabelName( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return "";
	}
	return pTbl->name;
}
/* set method */
static AST  setLabelName( AST index, PSZ value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, value );
		return 0;
	}
	if( value != NULL ){
		strncpy( pTbl->name, value, _BUFFER_SIZE_ );
	}else{
		pTbl->name[0]='\0';
	}
	return index;
}
#endif
//	IDX_LOCATION
static AST  getCodeScp( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->astCodeScp;
}
/* set method */
static AST  setCodeScp( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, value );
		return 0;
	}
	return pTbl->astCodeScp = value;
}
//	IDX_LOCATION
static AST  getNextOne( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->astNext;
}
/* set method */
static AST  setNextOne( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, value );
		return 0;
	}
	return pTbl->astNext = value;
}
static AST  getPrevOne( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->astPrev;
}
/* set method */
static AST  setPrevOne( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, value );
		return 0;
	}
	return pTbl->astPrev = value;
}
static AST  getLeftOne( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->astLeft;
}
/* set method */
static AST  setLeftOne( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, value );
		return 0;
	}
	return pTbl->astLeft = value;
}
static AST  getRightOne( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->astRight;
}
/* set method */
static AST  setRightOne( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, value );
		return 0;
	}
	return pTbl->astRight = value;
}
static AST  getFunction( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->astFunc;
}
/* set method */
static AST  setFunction( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, value );
		return 0;
	}
	return pTbl->astFunc = value;
}
/*debug,12/10/10,S*/
static BOOL  getFlgChg( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->flgChg;
}
static BOOL  setFlgChg( AST index, BOOL value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, value );
		return 0;
	}
	return pTbl->flgChg = value;
}
/*debug,12/10/10,E*/
const CMDLIST_ACCESS stCmdListAccess =
{
		//		BOOL (*clear)( VOID );
				ClearTable,
		//		BOOL (*reset)( VOID );
				ResetTable,
		//		BOOL (*alloc)( AST );
				AllocTable,
				getMaxLineNum,

				{
						getOpKind,
						setOpKind
				},
				{
						getCodeScp,
						setCodeScp
				},
				{
						getTypeInfo,
						setTypeInfo
				},
				{
						getLeftOne,
						setLeftOne
				},
				{
						getRightOne,
						setRightOne
				},
				{
						getNextOne,
						setNextOne
				},
				{
						getPrevOne,
						setPrevOne
				},
				{
						getFunction,
						setFunction
				},
				/*debug,12/10/10,S*/
				{
						getFlgChg,
						setFlgChg
				},
				/*debug,12/10/10,E*/
#ifdef RECORD_CMDLIST_LABEL
				{
						getLabelName,
						setLabelName
				}
#endif
};
/* end of file */
